﻿using System.Net.Http.Json;
using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.DataBase;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Config.RazorLib.Services
{
    public class DatabaseService : IDatabaseService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public DatabaseService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<PaginatedResult<DatabaseInfoResponseDto>?> QueryDatabasesByPage(
            DatabaseSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.Database.QueryDatabases));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<DatabaseInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<IList<DatabaseInfoResponseDto>>?> QueryDatabases(DatabaseSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.Database.QueryDatabases));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<IList<DatabaseInfoResponseDto>>>(resultJson);
                    }
                }
                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<DatabaseInfoResponseDto>?> UpdateDatabase(Guid key, DatabaseEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.Database.UpdateDatabase.Replace("{id}", key.ToString())}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<DatabaseInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<DatabaseInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<DatabaseInfoResponseDto>?> CreateDatabase(DatabaseEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response = await httpClient.PostAsJsonAsync($"/{ApiRoutes.Database.CreateDatabase}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<DatabaseInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<DatabaseInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<DatabaseInfoResponseDto>?> GetDatabase(Guid id)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("dataserverApi");
                var response =
                    await httpClient.GetAsync($"/{ApiRoutes.Database.GetDatabase.Replace("{id}", id.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<DatabaseInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}